package com.lhy.security;

import com.lhy.security.config.GRealm;
import com.lhy.security.core.GSecurity;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @description: 配置权限管理的demo
 * @author: 李汇远
 */
@Configuration
public class ConfigDemo implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 设置用户认证和授权的数据源
        GSecurity.setRealm(new GRealm() {
            @Override
            public String authentication(String userName) {
                // 测试账号 (admin , pwd)
                return "admin".equals(userName) ? GSecurity.getEncryptionPassword("pwd") : null;
            }

            @Override
            public HashSet<String> authorization(String userName) {
                return new HashSet<>(Arrays.asList("a,b".split(",")));
            }
        });
        // 将权限管理的拦截器添加到web中
        registry.addInterceptor(GSecurity.getInterceptor()).addPathPatterns("/**");
    }
}
